﻿
Imports Microsoft.VisualBasic
Imports System.IO
Imports System.IO.File
Imports System.Text
Public Class _error
    Private errorValue As String
    Private fatalValue As Boolean
    Private loggedValue As Boolean
    Private idValue As Integer
    Public Shared errorlist(0) As _error
    Public Shared log_location As String = My.Application.Info.DirectoryPath & "\logs\"
    Public Shared Event fatal_error()
    Public Sub New()
        MyBase.New()
    End Sub
    'Parameterized Constructor called only by Client Activated Objects
#Region "Constructors"
    Public Sub New(ByVal NewError As String, ByVal Newfatal As Boolean, ByVal NewLogged As Boolean, ByVal NewId As Integer)
        Me._error = NewError
        Me.fatal = Newfatal
        Me.logged = NewLogged
        Me.id = NewId
        add(Me)
    End Sub
    Public Sub New(ByVal NewError As String, ByVal Newfatal As Boolean, ByVal NewLogged As Boolean)
        Me._error = NewError
        Me.fatal = Newfatal
        Me.logged = NewLogged
        Me.id = getNextId()
        add(Me)
    End Sub
    Public Sub New(ByVal NewError As String)
        Me._error = NewError
        Me.fatal = False
        Me.logged = False
        Me.id = getNextId()
        add(Me)
    End Sub
#End Region
#Region "Properties"
    Public Property _error() As String
        Get
            Return errorValue
        End Get
        Set(ByVal Value As String)
            errorValue = Value
        End Set
    End Property
    Public Property fatal() As Boolean
        Get
            Return fatalValue
        End Get
        Set(ByVal Value As Boolean)
            fatalValue = Value
        End Set
    End Property
    Public Property logged() As Boolean
        Get
            Return loggedValue
        End Get
        Set(ByVal Value As Boolean)
            loggedValue = Value
        End Set
    End Property
    Public Property id() As Integer
        Get
            Return idValue
        End Get
        Set(ByVal Value As Integer)
            idValue = Value
        End Set
    End Property
#End Region
    Public Function getNextId() As Integer
        idInt = idInt + 1
        Return idInt
    End Function
    Sub add(ByVal __error As _error)
        ReDim Preserve errorlist(0 To UBound(errorlist) + 1)
        errorlist(__error.id) = __error
    End Sub
    Function geterrors()
        Return errorlist
    End Function
    Public Overrides Function ToString() As String
        If (logged) Then
            __log_error(_error)
        End If
        Return (vbNewLine & "Error: " & _error)
    End Function
    Public Shared Sub createError(ByVal __error As String)
        Dim temp As New _error(__error)
    End Sub
    ''' <summary>
    ''' Handles the passed error
    ''' </summary>
    ''' <param name="_e">error id</param>
    ''' <remarks>Use for declared errors</remarks>
    Public Shared Sub printerror(ByVal _e As Integer)
        Console.WriteLine(errorlist(_e)._error)
        If (errorlist(_e).logged) Then
            __log_error(_e)
        End If
        If (errorlist(_e).fatal) Then
            Console.Beep()
            RaiseEvent fatal_error()
        End If
    End Sub
    ''' <summary>
    ''' Handles a on-the-fly error
    ''' </summary>
    ''' <param name="_message">message to be printed</param>
    ''' <param name="logged">to add to log file</param>
    ''' <param name="fatal">will terminate program</param>
    ''' <remarks>use if error is not predifined</remarks>
    Public Shared Sub printerror(ByVal _message As String, ByVal logged As Boolean, ByVal fatal As Boolean)
        System.Console.ForegroundColor = ConsoleColor.DarkRed
        'System.Console.BackgroundColor = ConsoleColor.DarkCyan
        Console.WriteLine("Error: {0,5}", _message)
        If (logged) Then
            __log_error(_message)
        End If
        If (fatal) Then
            Console.Beep()
            RaiseEvent fatal_error()
        End If
        System.Console.ForegroundColor = ConsoleColor.Gray
    End Sub
    ''' <summary>
    ''' Logs a predefined error
    ''' </summary>
    ''' <param name="_error">error to be logged</param>
    ''' <remarks>Do not directly use this sub</remarks>
    Public Shared Sub __log_error(ByVal _error As Integer)

        Dim logfile As String = log_location & "\log.txt"
        My.Application.Log.WriteEntry(errorlist(_error)._error)
        Dim logline As String = vbCrLf & "(" & Date.Now.ToString & ")" & " Error: " & errorlist(_error)._error
        My.Computer.FileSystem.WriteAllText(logfile, logline, True)
        Dim myFileStream As FileStream
        Dim bteWrite() As Byte
        Dim intByte As Integer
        Dim lngLoop As Long

        Try
            intByte = Encoding.ASCII.GetBytes("asdf").Length
            ReDim bteWrite(intByte)
            bteWrite = Encoding.ASCII.GetBytes("asdf")
            myFileStream = File.OpenWrite("test.txt")

            For lngLoop = 0 To intByte - 1
                myFileStream.WriteByte(bteWrite(lngLoop))
            Next

            myFileStream.Close()
        Catch ex As IOException
            Console.WriteLine(ex.Message)
        End Try
    End Sub
    ''' <summary>
    ''' Logs a on-the-fly erorr
    ''' </summary>
    ''' <param name="_error">error to log</param>
    ''' <remarks>Do not directly use this sub</remarks>
    Public Shared Sub __log_error(ByVal _error As String)

        Dim logfile As String = log_location & "\" & Date.Now.Month & "-" & Date.Now.Day & "-" & Date.Now.Year & ".log"
        My.Application.Log.WriteEntry(_error)
        Dim logline As String = vbCrLf & "(" & Date.Now.ToString & ")" & " Error: " & _error

        Try
            If (Not My.Computer.FileSystem.DirectoryExists(log_location)) Then
                My.Computer.FileSystem.CreateDirectory(log_location)
            End If
            If (My.Computer.FileSystem.FileExists(logfile)) Then
                My.Computer.FileSystem.WriteAllText(logfile, logline, True)
            Else
                'create the file

                My.Computer.FileSystem.WriteAllText(logfile, logline, True)
            End If

        Catch ex As Exception
            printerror("Exception occurred in List: [" & ex.GetType().ToString() & "] " & ex.Message, True, False)
            printerror(vbTab & ex.StackTrace, True, False)
        End Try


    End Sub
End Class
